package com.vm.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.VMConst;
import com.vm.model.Goods;
import com.vm.model.UserAddress;
import com.vm.model.UserOrder;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderDetailActivity extends Activity {
	TextView tv_order_status;
	TextView tv_order_number;
	TextView tv_numberof;
	TextView tv_total_order_money;
	TextView tv_integra_num;
	TextView tv_total_goods_money;
	TextView tv_integra_num1;
	TextView tv_using_preferential_money;
	TextView tv_recipient_name;
	TextView tv_phone;
	TextView tv_address;
	TextView tv_note;
	TextView tv_goodsName;
	ImageView iv_goodsImage;
	ImageView imgbtn_search_back;
	Button btn_confirm_goods;
	String orderId;
	String goodsId;
	String addressId;
	Float price;
	int number;
	int integral;
	RequestQueue mQueue;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_details);
		orderId = getIntent().getStringExtra("orderId");
		mQueue = MyVolley.getRequestQueue();
		init();
	}

	public void init() {
		tv_order_status = (TextView) findViewById(R.id.tv_order_status);
		tv_order_number = (TextView) findViewById(R.id.tv_order_number);
		tv_numberof = (TextView) findViewById(R.id.tv_numberof);
		tv_total_order_money = (TextView) findViewById(R.id.tv_total_order_money);
		tv_integra_num = (TextView) findViewById(R.id.tv_integra_num);
		tv_total_goods_money = (TextView) findViewById(R.id.tv_total_goods_money);
		tv_integra_num1 = (TextView) findViewById(R.id.tv_integra_num1);
		tv_using_preferential_money = (TextView) findViewById(R.id.tv_using_preferential_money);
		tv_recipient_name = (TextView) findViewById(R.id.tv_recipient_name);
		tv_goodsName = (TextView) findViewById(R.id.tv_goodsName);
		tv_phone = (TextView) findViewById(R.id.tv_phone);
		tv_note = (TextView) findViewById(R.id.tv_note);
		tv_address = (TextView) findViewById(R.id.tv_address);
		iv_goodsImage = (ImageView) findViewById(R.id.iv_goodsImage);
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		btn_confirm_goods = (Button) findViewById(R.id.btn_confirm_goods);
		btn_confirm_goods.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (btn_confirm_goods.getText().toString().equals("返回")) {
					onBackPressed();
				} else {
					String status = "交易完成";
					try {
						status = URLEncoder.encode(status, "UTF-8");
					} catch (UnsupportedEncodingException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					String url = VMConst.REQUEST_URL
							+ "updateOrderStatus?orderId=" + orderId
							+ "&status=" + status;
					StringRequest stringRequest = new StringRequest(url,
							new Listener<String>() {

								@Override
								public void onResponse(String arg0) {
									// TODO Auto-generated method stub
									Toast toast = Toast.makeText(
											OrderDetailActivity.this, "确认收货",
											Toast.LENGTH_SHORT);
									toast.show();
									btn_confirm_goods.setText("返回");
									tv_order_status.setText("交易完成");
								}

							}, null);
					mQueue.add(stringRequest);
				}
			}
		});

		imgbtn_search_back.setOnClickListener(btn_backListener);
		initOrder();

	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	public void initOrder() {
		String url = VMConst.REQUEST_URL + "findOrderById?orderId=" + orderId;
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						// TODO Auto-generated method stub
						UserOrder userOrder = new UserOrder();
						userOrder = JsonUtils.jsonToBean(arg0, UserOrder.class);
						if (userOrder != null) {
							tv_order_number.setText(userOrder.getId());
							tv_order_status.setText(userOrder.getOrderStatus());
							if (userOrder.getNumber() != null) {
								tv_numberof.setText(Integer.toString(userOrder
										.getNumber()));
								number = userOrder.getNumber();
							}
							if (userOrder.getIntegral() != null) {
								tv_integra_num1.setText(Integer
										.toString(userOrder.getIntegral()));
								tv_integra_num.setText(Integer
										.toString(userOrder.getIntegral()));
								integral = userOrder.getIntegral();
								tv_using_preferential_money.setText(Float
										.toString(integral / 100));
							} else {
								tv_integra_num1.setText("0");
							}
							String status = "交易完成";
							if (tv_order_status.getText().toString()
									.equals(status)) {
								btn_confirm_goods.setText("返回");
							}
							goodsId = userOrder.getGoodsId();
							addressId = userOrder.getAddressId();
							initGoods();
							initAddress();
						}
					}

				}, null);
		mQueue.add(jsonObjectRequest);

	}

	public void initGoods() {
		String url = VMConst.REQUEST_URL + "goodsdetail?goodsId=" + goodsId;
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						Goods goods = new Goods();
						goods = JsonUtils.jsonToBean(arg0, Goods.class);
						if (goods != null) {
							tv_goodsName.setText(goods.getName());
							ImageLoader.getInstance().displayImage(
									VMConst.URL + goods.getImageUrl(),
									iv_goodsImage,
									MyDisplayImageOptions.getInstance());
							price = goods.getPrice();
							tv_total_goods_money.setText(Float.toString(price
									* number));
							tv_total_order_money.setText(Float.toString(price
									* number + 8 - integral / 100));
						}
					}
				}, null);
		mQueue.add(jsonObjectRequest);
	}

	public void initAddress() {
		String url = VMConst.REQUEST_URL + "findAddressById?addressId="
				+ addressId;
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						// TODO Auto-generated method stub
						UserAddress userAddress = new UserAddress();
						userAddress = JsonUtils.jsonToBean(arg0,
								UserAddress.class);
						if (userAddress != null) {
							tv_phone.setText(userAddress.getTelephone());
							tv_recipient_name.setText(userAddress.getName());
							tv_address.setText(userAddress.getAddress());
						}
					}

				}, null);
		mQueue.add(jsonObjectRequest);
	}
}
